package com.yyk.yiliao.ui.trade;


import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.listener.DialogSureCancelListenter;
import com.yyk.yiliao.listener.ToolbarListenter;
import com.yyk.yiliao.util.DialogUtil;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankMyaccountInfo;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的账户
 */
public class MyAccountActivity extends BaseActivity {


    @BindView(R.id.bt_invest)
    Button btInvest;
    @BindView(R.id.bt_draws)
    Button btDraws;
    @BindView(R.id.ll_myyhk)
    LinearLayout llMyyhk;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_bankcard)
    TextView tvBankcard;
    //判断是否设置交易密码
    private int typeFlag;
    //账户余额
    private float moneybalance;
    //判断是否绑定银行卡
    private int bankcountFlag;


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_account;
    }

    @Override
    public void initView() {
        initToolbar();

    }



    @Override
    protected void onResume() {
        super.onResume();
        initPost();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("我的账户");
        setMainTitleRightText("交易记录", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(MingxiActivity.class);
            }
        });
    }

    @Override
    public void initData() {

    }

    /**
     * 1.我的账户
     */
    private void initPost() {
        initUserbankMyaccount();
    }

    private void initUserbankMyaccount() {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid") + "");
        map.put("sign", Md5Util2.createSign(map));

        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankMyaccount(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<UserbankMyaccountInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("我的账户" + "异常");

                    }

                    @Override
                    public void onNext(UserbankMyaccountInfo info) {
                        Logger.e("我的账户" + info.toString());
                        if (info.getCode() == 1) {
                            UserbankMyaccountInfo.DataBean data = info.getData();
                            //钱
                            moneybalance = data.getMoney_balance();
                            tvMoney.setText("" + moneybalance);

//                            moneybalance = zhuanhuanFloat(money_balance);
//                            tickerView.setText("" + money_balance);
                            //是否绑定银行
                            bankcountFlag = data.getBank_count();
                            //是否设置交易密码
                            typeFlag = data.getType();
                            if (typeFlag == 0) {
                                DialogUtil.getIntanse().showReturnTips(mActivity, "", "为了您的账户安全，请先设置交易密码", "确认", "取消", new DialogSureCancelListenter() {
                                    @Override
                                    public void onSureListenter() {
                                        startAct(InvestPasswordActivity.class);
                                    }

                                    @Override
                                    public void onCancelListenter() {
                                        finish();
                                    }

                                }, true);
                            }
                            //设置多少张银行卡
                            tvBankcard.setText(bankcountFlag + "  张");
                        }
                    }
                });
    }


    /**
     * 1.充值
     * 2.提现
     * 3.我的银行卡
     *
     * @param view
     */
    @OnClick({R.id.bt_invest, R.id.bt_draws, R.id.ll_myyhk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_invest:
                startAct(RechargeActivity.class);
                break;
            case R.id.bt_draws:
                if (bankcountFlag == 0) {
                    DialogUtil.getIntanse().simpleDialog(mActivity, "添加银行卡", "暂无银行卡，请先添加本人银行卡", "确认", "取消", new DialogUtil.AlertDialogListenter() {
                        @Override
                        public void queding() {
                            startAct(AddBankcardActivity.class);

                        }

                        @Override
                        public void quxiao() {
                        }
                    }).show();
                } else {
                    startAct(DrawCashActivity.class);
                }


                break;
            case R.id.ll_myyhk:
                if (bankcountFlag == 0) {
                    DialogUtil.getIntanse().simpleDialog(mActivity, "添加银行卡", "暂无银行卡，请先添加本人银行卡", "确认", "取消", new DialogUtil.AlertDialogListenter() {
                        @Override
                        public void queding() {
                            startAct(AddBankcardActivity.class);

                        }

                        @Override
                        public void quxiao() {
                        }
                    }).show();
                } else {
                    startAct(MyBankcardActivity.class);
                }
                break;

        }
    }


}
